package org.rency.crawler.app.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import javax.annotation.Resource;
import org.rency.crawler.common.enums.HttpMethod;
import org.rency.crawler.common.helper.GeneratorHelper;
import org.rency.crawler.domain.FetchRequest;
import org.rency.crawler.fetch.processor.FetchProcessor;
import org.rency.crawler.fetch.repository.FetchPageRepository;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * 描述: ${TODO}<br>
 * 最近修改时间:2017/11/24 16:11<br>
 *
 * @author CaiyuRen
 * @version V1.0
 * @since 2017/11/24 16:11
 */
@RestController
@Api(value = "fetch", description = "爬虫抓取")
public class FetchController {

  @Resource
  private FetchProcessor                fetchProcessor;

  @Resource
  private FetchPageRepository           fetchPageRepository;

  @RequestMapping(value = "/fetch", method = RequestMethod.POST)
  @ApiOperation(value="提交抓取请求", notes = "提交抓取请求，默认使用GET方法")
  public String fetch(@RequestParam String host,@RequestParam String url) {
    FetchRequest request = new FetchRequest();
    request.setFetchUri(url.trim());
    request.setHostAddr(host.trim());
    request.setFetchNo(GeneratorHelper.genHashCode(request.getFetchUri()));
    request.setFetchHost(GeneratorHelper.genHashCode(request.getHostAddr()));
    request.setHttpMethod(HttpMethod.GET);
    new Thread(() -> fetchProcessor.fetch(request)).start();
    return "OK!";
  }

  @RequestMapping(value = "/fetch/page/count", method = RequestMethod.GET)
  @ApiOperation(value="已抓取页面数量", notes = "已抓取文档数量")
  public String fetchPageCount() {
    long count = fetchPageRepository.findCount();
    return "已抓取:"+count;
  }

}
